package com.sunwayworld.basemodule.common.eptstorage.util;

import bsh.EvalError;
import com.sunwayworld.framework.exception.checked.CheckedException;
import com.sunwayworld.framework.script.BshExecutor;
import com.sunwayworld.framework.utils.ObjectUtils;
import com.sunwayworld.framework.utils.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.atomic.AtomicInteger;

public class Util {
    private static final String paramKey = "$content$";

    public static BshExecutor getBshExecutor() {
        BshExecutor bsh = BshExecutor.instance();
        return bsh;
    }
    public static Object analysisBsh(String originalContent,String bshScript) throws EvalError {
        if(ObjectUtils.isEmpty(originalContent)){
            return "";
        }
        if(ObjectUtils.isEmpty(bshScript)){
            return originalContent;
        }
        BshExecutor bsh = getBshExecutor();
        bsh.eval("import java.text.*;");
        bsh.eval("import java.math.*;");
        bsh.eval("import java.lang.*");
        bsh.eval("import java.util.*");
        bsh.eval("import com.sunwayworld.framework.utils.StringUtils");
        bsh.eval("import com.sunwayworld.framework.utils.NumberUtils");
        bsh.eval("import com.sunwayworld.framework.utils.ObjectUtils");
        bsh.set(paramKey,originalContent); // $content$ 变量
        Object parseResult =  bsh.eval(bshScript);
        return parseResult;
    }

    private static final String SPACE = " ";
    /**
     * 字符串为 ：00 12 1A... 格式的16进制转化
     * @param hixArrayString
     * @return
     */
    public static byte[] hixStringToByteArray(String hixArrayString) {
        if (StringUtils.isEmpty(hixArrayString)) {
            return null;
        }
        String replace = StringUtils.replace(hixArrayString, SPACE, "");
        int length = replace.length();
        if (length % 2 != 0) {
            throw new CheckedException("message Length error :" + length);
        }
        byte[] dataArray = new byte[length / 2];
        AtomicInteger index = new AtomicInteger(0);
        for (int i = 0 ;i < replace.length(); i += 2) {
            dataArray[index.getAndIncrement()] = Integer.valueOf(replace.substring(i, i + 2), 16).byteValue();
        }
        return dataArray;
    }

    public static ByteBuf convert2Buffer(String msg) {
        byte[] sendByte = msg.getBytes();
        ByteBuf buffer = Unpooled.buffer(sendByte.length);
        buffer.writeBytes(sendByte);
        return buffer;
    }

    public static ByteBuf convert2Buffer(byte[] msg) {
        ByteBuf buffer = Unpooled.buffer(msg.length);
        buffer.writeBytes(msg);
        return buffer;
    }

    public static final String GB2312_CHART = "GB2312";
    public static final String UTF8_CHART = "UTF-8";
    public static String convert2String(ByteBuf msg,String chartsetName) {
        byte[] readByte = new byte[msg.readableBytes()];
        msg.readBytes(readByte);
        try {
            return new String(readByte, chartsetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public static byte[] convert2Byte(ByteBuf msg) {
        byte[] readByte = new byte[msg.readableBytes()];
        msg.readBytes(readByte);
        return readByte;
    }
}
